package net.zjitc.controller.admin;
import net.zjitc.vo.ProductVo;
import net.zjitc.commons.ServiceResp;
import net.zjitc.pojo.Category;
import net.zjitc.pojo.Products;
import net.zjitc.service.CategoryService;
import net.zjitc.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * @Author admin
 * @Date 2019/5/21 10:09
 */
@Controller
@RequestMapping("/admin/product")
public class ProductsController {
    @Autowired
    ProductService productService;
    @Autowired
    CategoryService categoryService;


//显示全部商品
    @RequestMapping(value = "/list")
    public String All_list(Model model){
        List<Products> list = productService.findAll();
        model.addAttribute("list",list);
        return "/admin/products/list.jsp";
    }
    //删除商品
    @RequestMapping(value = "/deleteProduct")
        public String deleteProduct(String id){
            productService.delete(id);
            return "/admin/product/list";
     }


     //编辑商品信息
        //通过id找商品
        @RequestMapping(value = "/findProductById")
        public String findProductById(String  id,Model model){
           Products products= productService.findById(id);
            List<Category> list = categoryService.findAll();
            model.addAttribute("clist",list);
            model.addAttribute("p",products);
            return "/admin/products/edit.jsp";
        }
        //修改更新
        @RequestMapping(value = "/update")
        @ResponseBody
        public ServiceResp update(Products products, HttpServletRequest request){
    //        if (file != null)
    //        {
    //            String path = request.getServletContext().getRealPath("/productss/6");
    //            if (!processFile(products,file,path))
    //            {
    //                return ServiceResp.createByERROR("商品修改失败");
    //            }
    //        }
            int update = productService.update(products);
            if (update>0)
            {
                return ServiceResp.createBySuccess("商品修改成功",0);
            }
            else {
                return ServiceResp.createByERROR("商品修改失败",1);
            }

        }

        //添加商品
            //进入到添加add.jsp页面

         @RequestMapping(value = "/addui")
         public String addui(Model model){
            List<Category> categoryList = categoryService.findAll();
            model.addAttribute("clist",categoryList);
            return "/admin/products/add.jsp";
        }
        //添加产品
    @RequestMapping(value = "/addProduct")
    @ResponseBody
    public ServiceResp addProduct( Products products,HttpServletRequest request){
        int count=productService.add(products);
        if (count>0){
            return ServiceResp.createBySuccess("商品添加成功！",0);
    }
        return ServiceResp.createByERROR("商品添加失败！",1);
    }

    //查询
    @RequestMapping(value = "/search")
    @ResponseBody
    public List<Products>search(ProductVo vo){
        List<Products> list = productService.search(vo);
        return list;
    }

//文件
    private boolean processFile(Products products,MultipartFile file,String path){
        if (file != null)
        {
            File dir = new File(path);
            if ((!dir.exists())||(!dir.isDirectory()))
            {
                dir.mkdirs();
            }
            String filename = UUID.randomUUID().toString()+"-"+file.getOriginalFilename();
            File filepath = new File(dir, filename);
            try {
                file.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            products.setImgurl("/productss/6/"+filename);
        }
        return true;
    }

}
